home *** CD-ROM | disk | FTP | other *** search
/ Belgian Amiga Club - ADF Collection / BS1 part 43.zip / Sources C- WorkDisk V.adf / iff / plopsound2.c < prev    next >
C/C++ Source or Header  |  1987-02-15  |  10KB  |  510 lines

  1. #include    <exec/types.h>
  2. #include     <exec/nodes.h>
  3. #include    <exec/libraries.h>
  4. #include        <exec/memory.h> /* soundprod */
  5.  
  6. #include    <graphics/gfx.h>
  7. #include    <graphics/rastport.h>
  8. #include        <graphics/sprite.h> /* voor sprites */
  9. #include    <graphics/text.h>
  10. #include    <intuition/intuition.h>
  11. #include     <stdio.h> /* ook voor soundprod */
  12. #include     "df1:iff/jiff.c"
  13.  
  14. #include "hardware/custom.h" /* soundprod */
  15.  
  16.  
  17. #define FRAMES        6
  18. #define SPRHEIGHT    16
  19. #define WORDSPERSPR    (2 * SPRHEIGHT + 4)
  20. #define MAXX        (XMAX-32)
  21. #define MAXY        (YMAX-16)
  22.  
  23. extern UWORD    ballmask[], ball0[];
  24. extern long    GetSprite();
  25.  
  26. UBYTE        idx[] = { 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4 };
  27. struct SimpleSprite    spr[FRAMES];
  28. UWORD        *sprbuf;
  29. UWORD        *sprites[FRAMES];
  30. int        vx[FRAMES], vy[FRAMES];
  31.  
  32.  
  33.  
  34. LONG filesize,s1,s2,per;
  35. char *filename;
  36. short *sndbuffer;
  37.  
  38.  
  39. /*This should be in exec/libraries.h */
  40. extern struct Library *OpenLibrary();
  41.  
  42. /*This should be in intuition/intuition.h */
  43. extern struct Screen *OpenScreen();
  44.  
  45. /* These are the bases for the libraries you need to do any graphics
  46.    much at all and still multi-task */
  47. struct Library *GfxBase = NULL;
  48. struct Library *LayersBase = NULL;
  49. struct Library    *IntuitionBase = NULL;
  50.  
  51. /* Well the workbench Screen isn't 320x200x5 so...*/
  52. struct Screen *PlopScreen = NULL;
  53.  
  54. /* Just one font for me... not static cause you might want it
  55.    in your menu modules. */
  56. struct TextAttr PlopFont =
  57.     {
  58.     "topaz.font",  /* I think this is the ROM font */
  59.     8,
  60.     0,
  61.     0,
  62.     };
  63.  
  64. /* a NewScreen - I don't know what half of this means either */
  65. static
  66. struct NewScreen PlopNewScreen =
  67.     {
  68.     0, 0, XMAX, YMAX, PLANES,
  69.     0, 1,
  70.     0,
  71.     CUSTOMSCREEN,
  72.     &PlopFont,
  73.     "Plop on Top",
  74.     NULL,
  75.     NULL,
  76.     };
  77.  
  78. /* To use clip blit need a RastPort.  Pretty useless, a BitMap should
  79.    be enough I think ... or anyway something a little less massive */
  80. struct RastPort PlopRastPort;
  81. struct ViewPort *vp;
  82.  
  83. void
  84. put_ea_cmap(cmap, colors)
  85. unsigned char *cmap;
  86. long colors;
  87. {
  88. long i;
  89. unsigned long r, g, b;
  90.  
  91. if (colors > 32)    /*color clipping*/
  92.     colors = 32;
  93.  
  94. /* This loop is modified to work with Aztec 3.4a 32bit by J. Van Houtven */
  95. /* Modification Date : 20 July 1987 */
  96.  
  97. for (i=0; i<colors; i++)
  98.     {
  99.          r= *cmap++ >> 4;
  100.          g= *cmap++ >> 4;
  101.          b= *cmap++ >> 4;
  102.          SetRGB4(&PlopScreen->ViewPort,i,(long)r,(long)g,(long)b);
  103.     }
  104. }
  105.  
  106.  
  107. main(argc, argv)
  108. int argc;
  109. char *argv[];
  110. {
  111. int i,j,x,y,offset = 0, flag = 0, speed;
  112. struct ILBM_info *info;
  113.  
  114. /* aanroep van sound routine */
  115.  
  116. custom.dmacon = 0x0003;
  117. per = 280;
  118. filename = "TAON_1";
  119. filesize = 42676;
  120. MakeSound();
  121.  
  122.  
  123. if ((GfxBase =  OpenLibrary("graphics.library", (long)0)) == NULL)
  124.         return(-1);
  125.  
  126. if ((LayersBase =  OpenLibrary("layers.library", (long)0)) == NULL)
  127.     {
  128.     plop_cleanup();
  129.     return(-2);
  130.     }
  131.  
  132. if ((IntuitionBase =  OpenLibrary("intuition.library",(long) 0)) == NULL)
  133.     {
  134.     plop_cleanup();
  135.     return(-3);
  136.     }
  137.  
  138. if ( (PlopScreen = OpenScreen(&PlopNewScreen) ) == NULL)
  139.     {
  140.     plop_cleanup();
  141.     return(-4);
  142.     }
  143. vp = &(PlopScreen->ViewPort);
  144.  
  145. InitRastPort(&PlopRastPort);
  146. PlopRastPort.Mask = (1<<PLANES)-1;
  147. /*just play it safe in case someone tries to load 6 bit planes...*/
  148.  
  149. setupsprites ();
  150.  
  151.  
  152. for (x=1; x<argc; x++)
  153. {
  154.  if ( (info = read_iff(argv[x], 0) ) != NULL)
  155.  {
  156.   put_ea_cmap(&info->cmap, (1 << info->bitmap.Depth));
  157.   PlopRastPort.BitMap = &info->bitmap;
  158.   ClipBlit( &PlopRastPort, (long)0, (long)0,
  159.            &PlopScreen->RastPort, (long)info->header.x, (long)info->header.y,
  160.        (long)info->header.w, (long)info->header.h, (long)COPY_MINTERM);
  161.  
  162.  for (i=0; i<FRAMES; i++)
  163.  {
  164.     if (GetSprite (&spr[i], (long) i+2) < 0)
  165.             plop_cleanup();
  166.     spr[i].x = 16*i;
  167.     spr[i].y = 16*i;
  168.     spr[i].height = SPRHEIGHT;
  169.     ChangeSprite (vp, &spr[i], sprites[i]);
  170.  }
  171.  speed = 4;
  172.  
  173.  for (i=0; i<FRAMES; i++)
  174.  {
  175.   vx[i]= vy[i]=speed;
  176.  }
  177.  
  178.  --speed;
  179.  
  180.  for(j=0;j<1000;j++)
  181.  {
  182.   WaitTOF ();
  183.   for (i=0; i<FRAMES; i++)
  184.   {
  185.    if((spr[i].x += vx[i]) >= MAXX-speed || spr[i].x <= speed)
  186.    {
  187.     vx[i] = -vx[i];
  188.    }
  189.  
  190.    if((spr[i].y += vy[i]) >= MAXY-speed || spr[i].y <= speed)
  191.    { 
  192.     vy[i] = -vy[i];
  193.    }
  194.   }
  195.  
  196.   /* Hey !!! ChangeSprite also MOVES the sprites !!!! */
  197.   for (i=0; i<FRAMES; i++) ChangeSprite (vp, &spr[i], sprites[idx[i+offset]]);
  198.  
  199.    /*  Rotate balls every other loop  */
  200.   if (flag = !flag) offset = (offset + 1) % 6;
  201.  
  202.  }
  203.  
  204.   /* scherm schuin */
  205.  
  206.   y = YMAX-1;
  207.   for(j=0;j<80;j++)
  208.   {
  209.    i=j<<2;
  210.    ScrollRaster(&PlopScreen->RastPort,0L,j-40L,i,0L,i+3L,y);
  211.   }
  212.  
  213.  for(j=0;j<1000;j++)
  214.  {
  215.   WaitTOF ();
  216.   for (i=0; i<FRAMES; i++)
  217.   {
  218.    if((spr[i].x += vx[i]) >= MAXX-speed || spr[i].x <= speed)
  219.    {
  220.     vx[i] = -vx[i];
  221.    }
  222.  
  223.    if((spr[i].y += vy[i]) >= MAXY-speed || spr[i].y <= speed)
  224.    { 
  225.     vy[i] = -vy[i];
  226.    }
  227.   }
  228.  
  229.   /* Hey !!! ChangeSprite also MOVES the sprites !!!! */
  230.   for (i=0; i<FRAMES; i++) ChangeSprite (vp, &spr[i], sprites[idx[i+offset]]);
  231.  
  232.    /*  Rotate balls every other loop  */
  233.   if (flag = !flag) offset = (offset + 1) % 6;
  234.  
  235.  }
  236.    /* cleanup sprites */
  237.   
  238.   for (i=0; i<FRAMES; i++)
  239.     if (spr[i].num) FreeSprite ((long) spr[i].num);
  240.  
  241.  
  242.  
  243.   SetAPen(&PlopScreen->RastPort,0);
  244.   for(j=0;j<100;j++)
  245.   {
  246.    Move(&PlopScreen->RastPort,0,j);Draw(&PlopScreen->RastPort,XMAX-1,j);
  247.    Move(&PlopScreen->RastPort,0,YMAX-1-j);Draw(&PlopScreen->RastPort,XMAX-1,YMAX-1-j);
  248.   }
  249.  
  250.   free_planes(&info->bitmap);
  251.   }
  252.  }
  253. plop_cleanup();
  254. }
  255. /* ====== end of main ======= */
  256.  
  257. plop_cleanup()
  258. {
  259. register int i;
  260.  
  261. custom.dmacon = 0x0003;
  262. if(sndbuffer)
  263.         FreeMem(sndbuffer,filesize);
  264.  
  265. for (i=0; i<FRAMES; i++)
  266.   if (spr[i].num) FreeSprite ((long) spr[i].num);
  267.  
  268. if (sprbuf)
  269.     FreeMem (sprbuf, 2L * WORDSPERSPR * FRAMES);
  270. if (PlopScreen != NULL)
  271.     CloseScreen(PlopScreen);
  272. if (IntuitionBase != NULL)
  273.     CloseLibrary(IntuitionBase);
  274. if (LayersBase != NULL)
  275.     CloseLibrary(LayersBase);
  276. if (GfxBase != NULL)
  277.     CloseLibrary(GfxBase);
  278. }
  279.  
  280.  
  281. MakeSound()
  282. {
  283. LoadSound();
  284. PlaySong();
  285. }  
  286.  
  287. LoadSound()
  288. {
  289. FILE *fopen(), *fp;
  290. SHORT *ptr;
  291. LONG i;
  292.  
  293. sndbuffer = AllocMem(filesize,MEMF_CHIP);
  294. ptr = sndbuffer;
  295.  
  296. custom . dmacon = 0x0003;
  297. fp = fopen(filename,"r");
  298. if (fp == NULL) return(0);
  299.  
  300. s1 = 0; s2 =filesize;
  301. for (i=0; i<s1; i++)
  302.     getc(fp);
  303.  
  304. for (i=s1/2; i<s2/2; i++)
  305.     {
  306.     *ptr++ = getc(fp)*256 + getc(fp);
  307.     }
  308.  
  309. fclose (fp);
  310. }
  311.  
  312. PlaySong()
  313. {
  314. custom . aud[0].ac_ptr = (UWORD *)sndbuffer;
  315. custom . aud[0].ac_len = s2/2-s1/2;
  316. custom . aud[0].ac_per = per;
  317. custom . aud[0].ac_vol = 64;
  318.  
  319. custom . aud[1].ac_ptr = (UWORD *)sndbuffer;
  320. custom . aud[1].ac_len = s2/2-s1/2;
  321. custom . aud[1].ac_per = per;
  322. custom . aud[1].ac_vol = 64;
  323.  
  324. custom . dmacon = 0x8203;
  325. }
  326.  
  327. setupsprites ()
  328. {
  329.     UWORD *cw;    /* current position in buffer of sprite images */
  330.     UWORD *maskp;    /* current position in ballmask             */
  331.     UWORD *ballp;    /* current position in ball0 data           */
  332.     int frame, scan;
  333.  
  334.     if (!(sprbuf = AllocMem (2L * WORDSPERSPR * FRAMES, MEMF_CHIP)))
  335.         plop_cleanup();
  336.  
  337.     cw = sprbuf;    /* current position at top of buffer */
  338.     ballp = ball0;    /* ... top of data to be interleaved */
  339.  
  340.     for (frame=0; frame<FRAMES; frame++) {
  341.         maskp = ballmask;    /* one mask for all frames */
  342.         sprites[frame] = cw;
  343.         *cw++ = 0;        /* poscntl */
  344.         *cw++ = 0;
  345.  
  346.         /* one word from ball0, one word from ballmask */
  347.         for (scan=0; scan<SPRHEIGHT; scan++) {
  348.             *cw++ = *maskp++;
  349.             *cw++ = *ballp++;
  350.         }
  351.         *cw++ = 0;    /* termination */
  352.         *cw++ = 0;
  353.     }
  354. }
  355.  
  356. #asm
  357. *:ts=8 bk=0
  358. _ballmask:
  359.     public    _ballmask
  360.  
  361.     dc.w    %0000011111100000
  362.     dc.w    %0001111111111000
  363.     dc.w    %0011111111111100
  364.     dc.w    %0111111111111110
  365.     dc.w    %0111111111111110
  366.     dc.w    %1111111111111111
  367.     dc.w    %1111111111111111
  368.     dc.w    %1111111111111111
  369.     dc.w    %1111111111111111
  370.     dc.w    %1111111111111111
  371.     dc.w    %1111111111111111
  372.     dc.w    %0111111111111110
  373.     dc.w    %0111111111111110
  374.     dc.w    %0011111111111100
  375.     dc.w    %0001111111111000
  376.     dc.w    %0000011111100000
  377.  
  378.  
  379. _ball0:
  380.     public    _ball0
  381.  
  382.     dc.w    %0000011001100000
  383.     dc.w    %0001100110011000
  384.     dc.w    %0011001111001100
  385.     dc.w    %0001110000111000
  386.     dc.w    %0011110000111100
  387.     dc.w    %0011100000011100
  388.     dc.w    %1100011111100011
  389.     dc.w    %1000011111100001
  390.     dc.w    %1000011111100001
  391.     dc.w    %1100011111100011
  392.     dc.w    %0011100000011100
  393.     dc.w    %0011110000111100
  394.     dc.w    %0001110000111000
  395.     dc.w    %0011001111001100
  396.     dc.w    %0001100110011000
  397.     dc.w    %0000011001100000
  398.  
  399.  
  400. _ball1:
  401.     public    _ball1
  402.  
  403.     dc.w    %0000010011000000
  404.     dc.w    %0001001100110000
  405.     dc.w    %0010011100011000
  406.     dc.w    %0001100001110010
  407.     dc.w    %0011100001111010
  408.     dc.w    %0111000001111001
  409.     dc.w    %1000111110000111
  410.     dc.w    %1000111110000011
  411.     dc.w    %1000111110000011
  412.     dc.w    %1000111110000111
  413.     dc.w    %0111000001111001
  414.     dc.w    %0011100001111010
  415.     dc.w    %0001100001110010
  416.     dc.w    %0010011100011000
  417.     dc.w    %0001001100110000
  418.     dc.w    %0000010011000000
  419.  
  420.  
  421. _ball2:
  422.     public    _ball2
  423.  
  424.     dc.w    %0000010011000000
  425.     dc.w    %0001001100110000
  426.     dc.w    %0000011000011000
  427.     dc.w    %0011000111100110
  428.     dc.w    %0010000111100010
  429.     dc.w    %0110000111110001
  430.     dc.w    %0001111000001110
  431.     dc.w    %0011111000001110
  432.     dc.w    %0011111000001110
  433.     dc.w    %0001111000001110
  434.     dc.w    %0110000111110001
  435.     dc.w    %0010000111100010
  436.     dc.w    %0011000111100110
  437.     dc.w    %0000011000011000
  438.     dc.w    %0001001100110000
  439.     dc.w    %0000010011000000
  440.  
  441.  
  442. _ball3:
  443.     public    _ball3
  444.  
  445.     dc.w    %0000000110000000
  446.     dc.w    %0000011001100000
  447.     dc.w    %0000110000110000
  448.     dc.w    %0110001111000110
  449.     dc.w    %0110001111000110
  450.     dc.w    %1100011111100011
  451.     dc.w    %0011100000011100
  452.     dc.w    %0111100000011110
  453.     dc.w    %0111100000011110
  454.     dc.w    %0011100000011100
  455.     dc.w    %1100011111100011
  456.     dc.w    %0100001111000010
  457.     dc.w    %0110001111000110
  458.     dc.w    %0000110000110000
  459.     dc.w    %0000011001100000
  460.     dc.w    %0000000110000000
  461.  
  462.  
  463. _ball4:        ; Batter takes first base
  464.     public    _ball4
  465.  
  466.     dc.w    %0000001100100000
  467.     dc.w    %0000110011001000
  468.     dc.w    %0001100001100000
  469.     dc.w    %0110011110001100
  470.     dc.w    %0100011110000100
  471.     dc.w    %1000111110000110
  472.     dc.w    %0111000001111000
  473.     dc.w    %0111000001111100
  474.     dc.w    %0111000001111100
  475.     dc.w    %0111000001111000
  476.     dc.w    %1000111110000110
  477.     dc.w    %0100011110000100
  478.     dc.w    %0110011110001100
  479.     dc.w    %0001100001100000
  480.     dc.w    %0000110011001000
  481.     dc.w    %0000001100100000
  482.  
  483.  
  484. _ball5:
  485.     public    _ball5
  486.  
  487.     dc.w    %0000001100100000
  488.     dc.w    %0000110011001000
  489.     dc.w    %0001100011100100
  490.     dc.w    %0100111000011000
  491.     dc.w    %0101111000011100
  492.     dc.w    %1001111000001110
  493.     dc.w    %1110000111110001
  494.     dc.w    %1100000111110001
  495.     dc.w    %1100000111110001
  496.     dc.w    %1110000111110001
  497.     dc.w    %1001111000001110
  498.     dc.w    %0101111000011100
  499.     dc.w    %0101111000011000
  500.     dc.w    %0001100011100100
  501.     dc.w    %0000110011001000
  502.     dc.w    %0000001100100000
  503.  
  504. #endasm
  505.  
  506.  
  507.  
  508.  
  509.  
  510.